package com.shitou.shitousearch;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.StringJoiner;

public class JDProductCrawler {
    public static void main(String[] args) throws IOException {
// 1. 发送HTTP请求获取京东页面的HTML内容
        String url = "https://search.jd.com/Search?keyword=c语言&page=2";
        String url2 = "https://search.jd.com/Search?keyword=手机&enc=utf-8&wq=手机&pvid=d367b26c894a4b62beb00cb7766f90d1&page=1";
        Document document = Jsoup.connect(url2).get();

// 2. 解析Document对象，找到商品信息所在的标签和属性，并提取出信息
        Elements elements = document.select(".gl-item");

        Elements ele = document.select("ps-main");


        for (Element element : elements) {

            String name = element.select(".p-name em").text(); // 商品名称
            String priceStr = element.select(".p-price i").text(); // 商品价格
            double price = Double.parseDouble(priceStr);

            String commentStr = element.select(".p-commit a").text(); // 商品评论数

            StringJoiner imgS = new StringJoiner("\n");

            Elements select = element.select(".ps-item");
            for (Element element1 : select) {
                imgS.add(element.select("a img").attr("data-lazy-img") + "\n");
            }

            // 3. 将提取出的信息输出到控制台
            System.out.println("名称: " + name);
            System.out.println("价格: " + price);
//            System.out.println("图片链接: " + imgUrl);
            System.out.println("图片: " + imgS);
            System.out.println();
        }
    }
}
